<template>
  <div class="login-container">
    <div class="login-box">
      <div class="close-btn" @click="goBack">
        <el-icon><Close /></el-icon>
      </div>
      
      <h1 class="login-title">用户注册</h1>
      
      <el-form
        ref="registerFormRef"
        :model="registerForm"
        :rules="rules"
        class="login-form"
      >
        <div class="form-label">用户名：</div>
        <el-form-item prop="username">
          <el-input
            v-model="registerForm.username"
            placeholder="请输入用户名"
          >
            <template #prefix>
              <el-icon><User /></el-icon>
            </template>
          </el-input>
        </el-form-item>
        
        <div class="form-label">密码：</div>
        <el-form-item prop="password">
          <el-input
            v-model="registerForm.password"
            type="password"
            placeholder="请输入密码"
            show-password
          >
            <template #prefix>
              <el-icon><Lock /></el-icon>
            </template>
          </el-input>
        </el-form-item>
        
        <div class="form-label">确认密码：</div>
        <el-form-item prop="confirmPassword">
          <el-input
            v-model="registerForm.confirmPassword"
            type="password"
            placeholder="请再次输入密码"
            show-password
          >
            <template #prefix>
              <el-icon><Lock /></el-icon>
            </template>
          </el-input>
        </el-form-item>
        
        <div class="form-label">邮箱：</div>
        <el-form-item prop="email">
          <el-input
            v-model="registerForm.email"
            placeholder="请输入邮箱"
          >
            <template #prefix>
              <el-icon><Message /></el-icon>
            </template>
          </el-input>
        </el-form-item>

        <div class="form-label">手机号：</div>
        <el-form-item prop="phone">
          <el-input
            v-model="registerForm.phone"
            placeholder="请输入手机号"
          >
            <template #prefix>
              <el-icon><Phone /></el-icon>
            </template>
          </el-input>
        </el-form-item>

        <div class="form-label">角色：</div>
        <el-form-item prop="permissionId">
          <el-radio-group v-model="registerForm.permissionId" class="role-group">
            <el-radio :label="1">会议组织者</el-radio>
            <el-radio :label="0">会议参与者</el-radio>
          </el-radio-group>
        </el-form-item>
        
        <el-button
          type="primary"
          class="submit-btn"
          :loading="loading"
          @click="handleRegister"
        >
          {{ loading ? '注册中...' : '注册' }}
        </el-button>
        
        <div class="form-footer">
          已有账号？
          <el-button link type="primary" @click="router.push('/login')" class="login-link">
            立即登录
          </el-button>
        </div>
      </el-form>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { User, Lock, Message, Close, Phone } from '@element-plus/icons-vue'
import { ElMessage } from 'element-plus'
import request from '@/utils/request'
import { useRouter } from 'vue-router'

const registerFormRef = ref(null)
const loading = ref(false)
const router = useRouter()

const registerForm = reactive({
  username: '',
  password: '',
  confirmPassword: '',
  email: '',
  phone: '',
  permissionId: null  // 修改 role 为 permissionId
})

const validatePass2 = (rule, value, callback) => {
  if (value === '') {
    callback(new Error('请再次输入密码'))
  } else if (value !== registerForm.password) {
    callback(new Error('两次输入密码不一致'))
  } else {
    callback()
  }
}

const rules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 3, max: 20, message: '长度在 3 到 20 个字符', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, max: 20, message: '长度在 6 到 20 个字符', trigger: 'blur' }
  ],
  confirmPassword: [
    { required: true, validator: validatePass2, trigger: 'blur' }
  ],
  email: [
    { required: true, message: '请输入邮箱地址', trigger: 'blur' },
    { type: 'email', message: '请输入正确的邮箱地址', trigger: 'blur' }
  ],
  phone: [
    { required: true, message: '请输入手机号', trigger: 'blur' },
    { pattern: /^1[3-9]\d{9}$/, message: '请输入正确的手机号', trigger: 'blur' }
  ],
  permissionId: [  // 修改 role 为 permissionId
    { required: true, message: '请选择角色', trigger: 'change' }
  ]
}

const handleRegister = async () => {
  if (!registerFormRef.value) return
  
  try {
    await registerFormRef.value.validate()
    loading.value = true
    
    const response = await request({
      url: '/api/auth/register',
      method: 'post',
      data: {
        username: registerForm.username,
        password: registerForm.password,
        email: registerForm.email,
        phone: registerForm.phone,
        permissionId: registerForm.permissionId === null ? null : Number(registerForm.permissionId)
      }
    })

    if (response.success) {
      ElMessage.success('注册成功！')
      router.push('/login')
    } else {
      ElMessage.error(response.message || '注册失败，请重试')
    }
  } catch (error) {
    console.error('注册失败:', error)
    ElMessage.error(error.response?.data?.message || '注册失败，请重试')
  } finally {
    loading.value = false
  }
}

const goBack = () => {
  console.log('返回上一页')
  router.back()
}
</script>

<style scoped>
.login-container {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(0, 0, 0, 0.5);
  backdrop-filter: blur(8px);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 2000;
}

.login-box {
  width: 400px;
  background: white;
  border-radius: 16px;
  padding: 40px;
  position: relative;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.1);
}

.close-btn {
  position: absolute;
  top: 16px;
  right: 16px;
  width: 32px;
  height: 32px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  background: rgba(0, 0, 0, 0.1);
  transition: all 0.3s ease;
}

.close-btn:hover {
  background: rgba(0, 0, 0, 0.2);
  transform: rotate(90deg);
}

.login-title {
  font-size: 32px;
  text-align: center;
  margin-bottom: 40px;
  background: linear-gradient(45deg, #1890ff, #36cfc9);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  font-weight: 600;
}

.form-label {
  font-size: 14px;
  color: #606266;
  margin-bottom: 8px;
  font-weight: 500;
}

.login-form :deep(.el-input__wrapper) {
  background-color: #f5f7fa;
  border-radius: 8px;
  height: 44px;
}

.login-form :deep(.el-input__inner) {
  height: 44px;
}

.login-form :deep(.el-input__prefix) {
  font-size: 18px;
  color: #909399;
}

.role-group {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.role-group :deep(.el-radio) {
  height: 44px;
  margin: 0;
  padding: 0 16px;
  border-radius: 8px;
  background-color: #f5f7fa;
  display: flex;
  align-items: center;
  transition: all 0.3s ease;
}

.role-group :deep(.el-radio.is-checked) {
  background-color: #e6f4ff;
}

.submit-btn {
  width: 100%;
  height: 44px;
  border-radius: 8px;
  font-size: 16px;
  font-weight: 500;
  margin: 20px 0;
  background: linear-gradient(45deg, #1890ff, #36cfc9);
  border: none;
}

.submit-btn:hover {
  opacity: 0.9;
  transform: translateY(-1px);
}

.form-footer {
  text-align: center;
  font-size: 14px;
  color: #606266;
}

.login-link {
  font-weight: 500;
  font-size: 14px;
}

@media screen and (max-width: 480px) {
  .login-box {
    width: 90%;
    padding: 30px 20px;
  }
  
  .login-title {
    font-size: 28px;
    margin-bottom: 30px;
  }
}
</style> 